// pages/yuyue/yuyue.js
var common = require("../../utils/Conmon.js");
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Name: '',
    Phone: '',
    Type: '',
    Demand: '', 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () { });
  },

  /**
    * 输入申请人
    */
  bindNameInput: function (event) {
    var that = this;
    that.setData({
      Name: event.detail.value
    })
  },

  /**
    * 输入手机号码
    */
  bindPhoneInput: function (event) {
    var that = this;
    that.setData({
      Phone: event.detail.value
    })
  },

  /**
    * 输入详细地址
    */
  bindTypeInput: function (event) {
    var that = this;
    that.setData({
      Type: event.detail.value
    })
  },

  /**
    * 输入备注
    */
  bindDemandInput: function (event) {
    var that = this;
    that.setData({
      Demand: event.detail.value
    })
  },


  /**
* 提交保存申请
*/
  bindSaveTap: function (event) {
    var that = this;
    var pattern = /^1[34578]\d{9}$/;
    if (that.data.Name == "" || that.data.Name == null) {
      wx.showModal({
        title: '警告',
        content: '请输入申请人',
        showCancel: false
      })
      return;
    }
    if (that.data.Phone == "" || that.data.Phone == null) {
      wx.showModal({
        title: '警告',
        content: '请输入手机号码',
        showCancel: false
      })
      return;
    }
    if (!pattern.test(that.data.Phone)) {
      wx.showModal({
        title: '警告',
        content: '号码不对,请重新输入',
        showCancel: false
      })
      return;
    }
    if (that.data.Type == "" || that.data.Type == null) {
      wx.showModal({
        title: '警告',
        content: '请输入项目类型',
        showCancel: false
      })
      return;
    }
    var param = {
      CustID: appData.custID, 
      Name: that.data.Name,
      Phone: that.data.Phone,
      Type: that.data.Type,
      Demand: that.data.Demand,
    };
    //加载数据绑定
    wx.showLoading({ title: '保存中...', mask: true });
    //console.log(param);//appData.apiurl + 'mobile/api/Home_Appointment/Add'
    that.wxSubRequest('POST', appData.apiurl + 'mobile/api/Home_Cooperation/Add', param,
      function (data) {
        if (data.code == 1200) {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '申请成功',
            showCancel: false
          })
          //that.setData({
          // CneeId: data.content.CneeID
          //})
          // var url = '/pages/deliveryAddr/deliveryAddr';
          // if (that.data.From == 'order' && that.data.IsDef == 1) {
          //   url = '/pages/submitOrder/submitOrder?CneeID=' + that.data.CneeId;
          // }
          //跳转页面
          wx.redirectTo({
            //url: appData.apiurl +"pages/my/my"
          })
        }
      },
      function (obj) {
        wx.hideLoading()
        console.log(obj)
      })
  },

  /**
   * 微信提交请求获取数据或保存数据
   * 
   */
  wxSubRequest: function (method, url, param, callBack, CallBackError) {
    wx.request({
      url: url,
      method: method,
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.code == 1200) {
          var data = res.data;
          callBack(data)
          console.log(res.data)
        }
      },
      fail: function (res) {
        CallBackError(res.data)
        console.log(res.data)
      },
      complete: function (res) {
        console.log(res.msg)
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})